package com.xw.controller.example;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xw.common.Const;
import com.xw.model.sdk.AliPayModel;
import com.xw.model.sdk.AliPayPCModel;
import com.xw.model.sdk.WeChatModel;
import com.xw.sdk.wechat.WXPay;
import com.xw.sdk.wechat.WXPayUtil;
import com.xw.service.tool.alipay.AlipayService;
import com.xw.service.tool.wechat.WeChatService;
import com.xw.util.AppUtil;
import com.xw.util.PropertiesUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付测试案例
 * app支付
 */
@Controller
@RequestMapping("/payTest")
@Scope("prototype")
public class PayTest {
    @Resource
    private AlipayService alipayService;
    @Resource
    private WeChatService weChatService;

    @RequestMapping(value = "/toAliPayPC",method = RequestMethod.POST)
    @ResponseBody
    public Object toAliPayPC(HttpServletRequest request) throws Exception{
        AliPayPCModel model = new AliPayPCModel();
        model.setBody("我是测试数据1");
        model.setSubject("App支付测试Java");
        model.setOutTradeNo(AppUtil.getOrderCode());
        //支付宝以元为单位
        model.setTotalAmount("0.01");
        model.setNotifyUrl(Const.WEB_URL+"aliPayCallBack/test");
        Map<String, Object> map = alipayService.toPCPay(model);
        System.out.println(map);
        return JSONObject.toJSON(map);
    }

    /**
     * 支付宝app
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/toPay",method = RequestMethod.POST)
    @ResponseBody
    public Object pay(HttpServletRequest request) throws Exception{
        AliPayModel model = new AliPayModel();
        model.setBody("我是测试数据");
        model.setSubject("App支付测试Java");
        model.setOutTradeNo(AppUtil.getOrderCode());
        //支付宝以元为单位
        model.setTotalAmount("0.01");
        model.setNotifyUrl(Const.WEB_URL+"aliPayCallBack/test");
        Map<String, Object> map = alipayService.toPay(model);
        System.out.println(map);
        return JSONObject.toJSON(map);
    }

    /**
     * 微信
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/toWeChatPay",method = RequestMethod.POST)
    @ResponseBody
    public Object toWeChatPay(HttpServletRequest request) throws Exception{
        WeChatModel weChatModel = new WeChatModel();
        weChatModel.setBody("test");
        //微信以分为单位
        weChatModel.setTotal_fee(1);
        weChatModel.setNotify_url(Const.WEB_URL+"weChatCallBack/test");
        weChatModel.setOut_trade_no(AppUtil.getOrderCode());

        Map<String, Object> map = weChatService.toNativePay(weChatModel,request);
        return JSONObject.toJSON(map);
    }

    @RequestMapping(value = "/getSign",method = RequestMethod.POST)
    @ResponseBody
    public Object getSign(HttpServletRequest request) throws Exception{
            try {
                Map<String, String> params = new HashMap<String, String>();
                params.put("mch_id", PropertiesUtil.getProperty("WEICHAT_MCHID"));
                params.put("nonce_str", WXPayUtil.generateNonceStr());
                params.put("sign", WXPayUtil.generateSignature(params, PropertiesUtil.getProperty("WEICHAT_APIKEY")));
                String strXML = HttpUtil.post("https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey", String.valueOf(params));
                if (StringUtils.isBlank(strXML)) {
                    return null;
                }
                Map<String, String> result = WXPayUtil.xmlToMap(strXML);
                if ("SUCCESS".equals(result.get("return_code"))) {
                    return result.get("sandbox_signkey");
                }
                return null;
            } catch (Exception e) {
                return null;
            }

    }




}
